package com.ziyue.socket.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ziyue.socket.bean.Member;
import com.ziyue.socket.bean.Room;
import com.ziyue.socket.bean.activity.Activity;
import com.ziyue.socket.bean.test.TestRequest;
import com.ziyue.socket.enums.OptionEnum;
import com.ziyue.socket.exception.UnFullWebsocketMessageException;
import com.ziyue.socket.service.OptionAbstract;
import io.netty.channel.Channel;

import java.util.List;

/**
 * Created by wancheng on 2019/4/18.
 */
public class TestStart extends OptionAbstract {

    private final OptionEnum OPTION = OptionEnum.TEST_START;
    @Override
    public Object send(Channel channel, String requestBody) throws Exception {
        TestRequest testRequest = JSONObject.parseObject(requestBody,TestRequest.class);

        if (testRequest.getActivityId() == null){
            throw  new UnFullWebsocketMessageException("无互动id");
        }
        if (testRequest.getActionTime() == null){
            throw  new UnFullWebsocketMessageException("开始时间为空");
        }
        if (testRequest.getQuestionCount() == null){
            throw  new UnFullWebsocketMessageException("题目总数为空");
        }
        Room room = roomManage.getRoom(channel);
        Activity activity = new Activity();
        activity.setActivityId(testRequest.getActivityId());
        activity.setQuestionCount(testRequest.getQuestionCount());
        activity.setStartTime(testRequest.getActionTime());
        if (testRequest.getLimit() != null){
            activity.setLimit(testRequest.getLimit());
            //activity.setEndTime();
        }

        room.resetActivity(activity);
        room.setCurrentActivityId(testRequest.getActivityId());
        //发送给学生的信息
        List<Member> targetGroups = roomManage.targetGroups(channel);
        this.sendMsg(channels, targetGroups,"开始答题",testRequest,OPTION);
        //this.saveActionLog(channel,testRequest.getActivityId(),testRequest.getActionTime(), RoomActionEnum.START);
        return null;
    }

    @Override
    public Object get(Channel channel, String requestBody) {
        return null;
    }
}
